'use strict';

angular.module('com.dcos.app').controller('WorkFlowController', function($window,$rootScope,$scope,$state, $filter, api, $uibModal, WorkFlowService,WorkFlowBuildService) {
  $scope.initPageSize = api.initPageSize;
  $scope.pipelineData=[];

  $scope.curProjectId=$window.sessionStorage.getItem("curProjectId");

  $scope.userid=$window.sessionStorage.getItem("curUserId");

  // if(!vm.userid)
  //   $state.go('login');
  $scope.msg="删除操作不可恢复，确定要删除？";
  
  $scope.getList = function () {
    $scope.loading = false;
//  WorkFlowService.getlist($scope.curProjectId).then(function (response) {
		initIndexedDB("workflowsql",function(){
			var transaction = indexeddb.transaction("job",'readwrite');
			var store = transaction.objectStore('job');
			var request = store.openCursor();
			$scope.pipelineData = [];
			request.onsuccess = function(e){
				var cursor = e.target.result;
				if(cursor){
					$scope.pipelineData.push(cursor.value);
					cursor.continue();
				}else{
					$scope.page = 1;
					$scope.pageSize = $scope.initPageSize;
					$scope.totalCount = $scope.pipelineData.length;
					$scope.itemLength = $scope.totalCount;
					$scope.sortKey = "create_time";
					$scope.reverse = false;
				}
			}
//    if(response.data.data!=null){
//      $scope.pipelineData=response.data.data;
//    }
//    else
//    $scope.pipelineData=[];
//    $scope.page = 1;
//
//    $scope.pageSize = $scope.initPageSize;
//    $scope.totalCount = $scope.pipelineData.length;
//    $scope.itemLength = $scope.totalCount;
//
//    $scope.sortKey = "create_time";
//    $scope.reverse = false;
    });
}

	$scope.getList();

  $scope.changePager = function () {
    var filtered = $filter('filter')($scope.pipelineData, {name: $scope.query});
    $scope.totalCount = filtered.length;
  }
  $scope.ok = function(p_id) {
    $rootScope.loading = true;
//  WorkFlowService.delete(p_id).then(function (response) {
		initIndexedDB("workflowsql",function(){
			var transaction = indexeddb.transaction("job",'readwrite');
			var store = transaction.objectStore('job');
			var index = store.index('p_id');
			var range = IDBKeyRange.only(p_id);
			index.openCursor(range).onsuccess = function(e){
				var cursor = e.target.result;
				if(cursor){
					var deleteRequest = cursor.delete();
					deleteRequest.onsuccess = function(e){
						$rootScope.loading = false;
      			$scope.getList();
					}
				}
			}
		});
//  });
  }
  $scope.bulid = function (status,p_id) {
//  if(status=="1"){
//    WorkFlowService.build(p_id).then(function (response) {
//      $state.go('app.workflow.build', {'p_id':p_id});
//    });
//  }else{
      $state.go('app.workflow.build', {'p_id':p_id});
//  }

  }
  $scope.add = function () {
    $state.go('app.workflow.create', {'p_id':null});
  }
  $scope.edit = function (p_id) {
    //$state.go('app.workflow.edit', {'p_id':p_id});
    $state.go('app.workflow.build', {'p_id':p_id});
  }
  $scope.createByTemplate = function () {
    var modalInstance = $uibModal.open({
      animation: true,
      templateUrl: '../../controllers/workflow/template/template.html',
      controller: 'TemplateSelectController as vm',
      resolve: {
        
      }
    });
  }
  $scope.stop = function (p_id) {
    WorkFlowBuildService.stop(p_id).then(function (response) {
      $scope.getList();
    });
  }
});

